{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding Constraints to Tasks and Workflows\n",
    "\n",
    "Add constraints and execution directives to electrons by specifying them as arguments to the Covalent `@electron` decorator.\n",
    "\n",
    "The most-used constraint is to explicitly name an executor for an electron. Other constraints are described in the [API Reference](../api/electrons.rst)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prerequisites\n",
    "\n",
    "[Create a lattice](./construct_lattice.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "\n",
    "@ct.electron\n",
    "def sum(x, y):\n",
    "    return x + y\n",
    "\n",
    "@ct.electron\n",
    "def square(x):\n",
    "    return x * x\n",
    "\n",
    "@ct.lattice\n",
    "def sum_of_squares_wf(a, b):\n",
    "    x2 = square(a)\n",
    "    y2 = square(b)\n",
    "    return sum(x=x2, y=y2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Procedure\n",
    "\n",
    "1. To specify a constraint on a single electron, add the constraint as a keyword argument to its Covalent `@electron` decorator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.electron(executor = \"local\")\n",
    "def sum(x, y):\n",
    "    return x + y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. To specify a constraint on all electrons in a lattice, add the constraint as a keyword argument to the `@lattice` decorator. The constraint is inherited by all electrons in the lattice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.lattice(executor = \"local\")\n",
    "def sum_of_squares_wf(a, b):\n",
    "    x2 = square(a)\n",
    "    y2 = square(b)\n",
    "    return sum(x=x2, y=y2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. To override a constraint inherited from a lattice, specify the overriding constraint on the `@electron` decorator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "\n",
    "@ct.electron(executor = \"dask\")\n",
    "def sum(x, y):\n",
    "    return x + y\n",
    "\n",
    "@ct.electron\n",
    "def square(x):\n",
    "    return x * x\n",
    "\n",
    "@ct.lattice(executor = \"local\")\n",
    "def sum_of_squares_wf(a, b):\n",
    "    x2 = square(a)\n",
    "    y2 = square(b)\n",
    "    return sum(x=x2, y=y2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the example above, the `square` electron inherits the `local` executor from the lattice. The `sum` electron overrides the inherited value and uses the `dask` executor instead.\n",
    "\n",
    "### See Also\n",
    "\n",
    "[Adding Pip Dependencies to an Electron](./add_pip_dependencies_to_electron.ipynb)\n",
    "\n",
    "[Adding Bash Dependencies to an Electron](./add_bash_dependencies_to_electron.ipynb)\n",
    "\n",
    "[Adding Callable Function Dependencies to an Electron](./add_callable_dependencies_to_electron.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
